﻿using System;
using RedmineTypes = Redmine.Net.Api.Types;

namespace Redmine.Laptop.API.Models
{
    public class Issue
    {
        public int IssueID { get; set; }
        public Project Project { get; set; }
        public IssueStatus Status { get; set; }
        public IssuePriority Priority { get; set; }
        public User Author { get; set; }
        public string CategoryName { get; set; }
        public string Subject { get; set; }
        public string Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime? DueDate { get; set; }
        public float? DoneRatio { get; set; }
        public float? EstimatedHours { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime UpdatedDate { get; set; }
        public User AssignedUser { get; set; }
        public Issue ParentIssue { get; set; }

        public static explicit operator RedmineTypes::Issue(Issue issue)
        {
            return new RedmineTypes::Issue
            {
                AssignedTo = issue.AssignedUser != null ? new RedmineTypes::IdentifiableName { Id = issue.AssignedUser.UserID, Name = issue.AssignedUser.UserName } : null,
                Id = issue.IssueID,
                CreatedOn = issue.CreatedDate,
                Description = issue.Description,
                Project = new RedmineTypes::IdentifiableName { Id = issue.Project.ProjectID, Name = issue.Project.ProjectName },
                Priority = new RedmineTypes::IdentifiableName {Id = issue.Priority.PriorityID, Name = issue.Priority.PriorityName},
                StartDate = issue.StartDate,
                Status = new RedmineTypes::IdentifiableName { Id = issue.Status.StatusID, Name = issue.Status.StatusName },
                Subject = issue.Subject,
                UpdatedOn = issue.UpdatedDate,
                ParentIssue = issue.ParentIssue != null ? new RedmineTypes::IdentifiableName { Id = issue.ParentIssue.IssueID, Name = issue.ParentIssue.Subject } : null,
                EstimatedHours = issue.EstimatedHours,
                DoneRatio = issue.DoneRatio,
                Author = new RedmineTypes::IdentifiableName { Id = issue.Author.UserID, Name = issue.Author.UserName },
                DueDate = issue.DueDate,
                //Category = 
            };
        }

        public static explicit operator Issue(RedmineTypes::Issue issue)
        {
            return new Issue
            {
                AssignedUser = issue.AssignedTo != null
                                ? new User { UserID = issue.AssignedTo.Id, FirstName = issue.AssignedTo.Name.Split(' ')[0], LastName = issue.AssignedTo.Name.Split(' ')[1] }
                                : null,
                Author = new User { UserID = issue.Author.Id, FirstName = issue.Author.Name.Split(' ')[0], LastName = issue.Author.Name.Split(' ')[1] },
                CreatedDate = issue.CreatedOn.Value,
                Description = issue.Description,
                //CategoryName =
                DoneRatio = issue.DoneRatio,
                Project = new Project {ProjectID = issue.Project.Id, ProjectName = issue.Project.Name},
                DueDate = issue.DueDate,
                EstimatedHours = issue.EstimatedHours,
                IssueID = issue.Id,
                ParentIssue = issue.ParentIssue != null ? new Issue {IssueID = issue.ParentIssue.Id, Subject = issue.ParentIssue.Name} : null,
                Priority = new IssuePriority {PriorityID = issue.Priority.Id, PriorityName = issue.Priority.Name},
                StartDate = issue.StartDate.Value,
                Subject = issue.Subject,
                Status = new IssueStatus {StatusID = issue.Status.Id, StatusName = issue.Status.Name },
                UpdatedDate = issue.UpdatedOn.Value
            };
        }
    }
}
